package com.example.mobifin_busticket.views.activities;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

import javax.crypto.AEADBadTagException;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

import com.example.mobifin_busticket.AppData;
import com.example.mobifin_busticket.MyApplication;
import com.example.mobifin_busticket.R;
import com.example.mobifin_busticket.controller.databasemanager.DatabaseManager;
import com.example.mobifin_busticket.models.SourceModel;
import com.example.mobifin_busticket.views.adapter.CustomListAdapter;
import com.example.mobifin_busticket.views.adapter.RecentCityAdapter;

public class SelectDestinationCityActivity extends MobifinActivity implements
		OnClickListener {
	ListView listview, recentSourceList;
	CustomListAdapter CAdapter;
	RecentCityAdapter recentCityAdapter;
	SourceModel recentSourceModel;
	EditText et_destination_search;
	Button btn_back, btn_clr;
	TextView tvToCity;
	ArrayList<SourceModel> recentCityList;
	ArrayList<SourceModel> destinationList;
	LinearLayout layout_rcnt_bus, ll_slctBus;
	 DatabaseManager dbmanManager = MyApplication.getDatabaseManagerInstance();
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.select_city_activity);
		recentSourceModel = new SourceModel();
		String forwhich = getIntent().getStringExtra("FOR");
		init();
		if (forwhich.equalsIgnoreCase("Source")) {
			/*DatabaseManager dbmanManager = new DatabaseManager(
					SelectDestinationCityActivity.this);*/
			destinationList = dbmanManager.getSourceFromDB();
			dbmanManager.close();
			System.out.println("Sourcr list size:" + destinationList.size());
			CAdapter = new CustomListAdapter(this, destinationList);
			listview.setAdapter(CAdapter);
		} else if (forwhich.equalsIgnoreCase("Destination")) {
			destinationList = AppData.list;
			System.out.println("Sourcr list size:" + destinationList.size());
			CAdapter = new CustomListAdapter(this, destinationList);
			listview.setAdapter(CAdapter);
		}
		listview.setOnItemClickListener(new OnItemClickListener() {
			/*DatabaseManager dbmanManager = new DatabaseManager(
					SelectDestinationCityActivity.this);*/

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				SourceModel sm = (SourceModel) listview.getAdapter().getItem(
						position);
				String destination_name = sm.getName();
				recentSourceModel.setRecentCityId(sm.getId());
				recentSourceModel.setRecentCityName(destination_name);
				// dbmanManager.deleteFromRecentSearch("Baroda");
				dbmanManager.insertRecentCiry(
						recentSourceModel.getRecentCityId(),
						recentSourceModel.getRecentCityName());
				Intent returnIntent = new Intent();
				returnIntent.putExtra("DresultName", destination_name);
				returnIntent.putExtra("DresultID", sm.getId());
				setResult(RESULT_OK, returnIntent);
				finish();
				overridePendingTransition(R.anim.hold,
						R.anim.push_out_to_bottom);
				
			}

		});

		DatabaseManager dbmanManager = new DatabaseManager(
				SelectDestinationCityActivity.this);
		if (dbmanManager.checkRecentSources()) {
			if (recentCityList != null) {
				recentCityList.clear();
			}
			valueInRecentCityList();
		}
		if (recentCityList != null) {
			layout_rcnt_bus.setVisibility(View.VISIBLE);
		} else {
			layout_rcnt_bus.setVisibility(View.GONE);
		}
		et_destination_search.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub
				btn_clr.setVisibility(View.VISIBLE);
				SelectDestinationCityActivity.this.CAdapter.getFilter().filter(
						s.toString());
				layout_rcnt_bus.setVisibility(View.GONE);
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
			}
		});
		dbmanManager.close();
	}

	private void init() {
		// TODO Auto-generated method stub
		listview = (ListView) findViewById(R.id.lv_cityList);
		recentSourceList = (ListView) findViewById(R.id.iv_recntSerach);
		et_destination_search = (EditText) findViewById(R.id.et_city_nm);
		tvToCity = (TextView) findViewById(R.id.txt_from_to_city);
		tvToCity.setText(getResources().getString(R.string.text_desc));
		btn_back = (Button) findViewById(R.id.header_back_btn);
		btn_clr = (Button) findViewById(R.id.btn_clear);
		layout_rcnt_bus = (LinearLayout) findViewById(R.id.layout_rcnt_bus);
		ll_slctBus = (LinearLayout) findViewById(R.id.layout_list);
		recentCityList = new ArrayList<SourceModel>();
		destinationList = new ArrayList<SourceModel>();
		btn_back.setOnClickListener(this);
		btn_clr.setOnClickListener(this);
	}

	protected void valueInRecentCityList() {
		// TODO Auto-generated method stub
		/*DatabaseManager db = new DatabaseManager(
				SelectDestinationCityActivity.this);*/
		recentCityList = dbmanManager.getRecentSourceFromDB();

		System.out.println("Source list size:" + recentCityList.size());

		recentCityAdapter = new RecentCityAdapter(this, recentCityList);
		recentSourceList.setAdapter(recentCityAdapter);
		recentSourceList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				SourceModel sm = (SourceModel) recentSourceList.getAdapter()
						.getItem(position);
				String destination_name = sm.getRecentCityName();
				Intent returnIntent = new Intent();
				returnIntent.putExtra("DresultName", destination_name);
				returnIntent.putExtra("DresultID", sm.getRecentCityId());
				setResult(RESULT_OK, returnIntent);
				finish();
				overridePendingTransition(R.anim.hold,
						R.anim.push_out_to_bottom);
			}
		});
		//db.close();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.header_back_btn:
			finish();
			break;
		case R.id.btn_clear:
			onClickBtnClear();
			break;

		default:
			break;
		}
	}

	private void onClickBtnClear() {
		// TODO Auto-generated method stub
		et_destination_search.setText("");
		if (recentCityList != null) {
			layout_rcnt_bus.setVisibility(View.VISIBLE);
		} else {
			layout_rcnt_bus.setVisibility(View.GONE);
		}
		btn_clr.setVisibility(View.GONE);
		if (CAdapter != null) {
			SelectDestinationCityActivity.this.CAdapter.getFilter().filter("");
		}
	}
}